﻿myApp.controller("CareerController", function ($scope, $http, $modal, modalService, $sce, ngTableParams, $filter) {
    document.title = "Penang Realty 世界地產 CAREER";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 CAREER', 'description': 'Earn more than 10K per month, Join us!' });
    function pageInit() {
        // get description
        $http.get('api/Career/GetJobPositions')
             .success(function (data) {

                 $scope.job = data;
             }
        )

        $http.get('api/Career/GetJobDescription')
            .success(function (data) {

            $scope.description = data;
            }
        )
        // get job vacancy

        $scope.loader = { loading: false };

    }
    pageInit();
    $scope.person = {

    };

    $scope.save = function () {
        $scope.loader.loading = true;


        window.scrollBy(0, -10000);
        if (!validation())
            return;

        url = 'api/Career/save';
        
        $http.post(url, $scope.person)
             .success(function (result) {
                 $scope.loader.loading = false;
                 $scope.data.searchData = result;
                 $scope.info = "Your request has been submitted successfully. We thank you for your application.";

             })
             .error(function (result) {
                 $scope.loader.loading = false;
                 $scope.error = "Your request cannot be submitted now. Please contact our office directly for us to liase with you immediately.";
             }
         )
    }


    $scope.clearEntry = function () {
        $scope.person.name = "";
        $scope.person.phone = "";
        $scope.person.gender = "";
        $scope.person.ic = "";
        $scope.person.address = "";
        $scope.person.position = "";
        $scope.close();
    }

    $scope.close = function () {
        $scope.warning = null;
        $scope.info = null;
        $scope.error = null;
    }

    function validation() {
        $scope.warning = [];

        if (!$scope.person.name)
            $scope.warning.push({ msg: "Name must not be empty!" });
        if (!$scope.person.phone)
            $scope.warning.push({ msg: "Phone must not be empty!" });
        if (!$scope.person.ic)
            $scope.warning.push({ msg: "IC must not be empty!" });
        
        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }
})